/*** This do-file converts NIPA flat files to Stata dta.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Create required subfolders
cap mkdir "${root}/data/derived/NIPA"

project, uses("${root}/data/dvc/NIPA/source.yaml") reference

cap program drop load_nipa
program define load_nipa
	
	syntax anything, freq(string) [omitseries(string)]

	* Parse the specified NIPA table number
	tokenize `anything', parse(".")
	local table = "T" + "`1'" + strofreal(`3', "%02.0f") + strofreal(`5', "%02.0f")
	
	* Load list of series in specified NIPA table
	project, uses("${root}/data/dvc/NIPA/SeriesRegister.txt")
	import delimited using "${root}/data/dvc/NIPA/SeriesRegister.txt", clear

	keep if strpos(tableidlineno,"`table'") > 0

	if ("`omitseries'"!="") {
		foreach series of local omitseries {
			drop if seriescode == "`series'"
		}
	}

	gisid seriescode
	tempfile serieslist
	save `serieslist'

	* Load all data series in specified NIPA table from Flat File
	project, uses("${root}/data/dvc/NIPA/nipadata`freq'.txt")
	import delimited using "${root}/data/dvc/NIPA/nipadata`freq'.txt", clear varnames(1)

	merge m:1 seriescode using `serieslist', assert(1 3) keep(3) nogen

	* Parse period string into numeric format
	if ("`freq'"=="Q") {
		gen int quarter = quarterly(period, "YQ")
		format %tq quarter
		local timevars quarter
	}
	else if ("`freq'"=="M") {
		gen int year = real(substr(period, 1, 4))
		gen byte month = real(substr(period, 6, 2))
		local timevars year month
	}
	drop period
	
	* Destring value
	replace value = subinstr(value,",","",.)
	destring value, replace
	
	order seriescode serieslabel `timevars'
	sort seriescode `timevars'
	gisid seriescode `timevars'
	
end

*-------------------------------------------------------------------------------
* NIPA Table 1.1.1 Percent Change From Preceding Period in Real Gross Domestic Product
*-------------------------------------------------------------------------------

* Load data
load_nipa 1.1.1, freq(Q)
drop tableidlineno seriescodeparents  // not useful to us

* Label value
assert metricname == "Fisher Quantity Index" | (serieslabel == "Gross domestic product, current dollars" & metricname == "Current Dollars")
assert calculationtype == "Percent change, annual rate"
assert defaultscale == 0
label var value "Percent change, annual rate"
drop metricname calculationtype defaultscale // constant values

* Output
save "${root}/data/derived/NIPA/NIPA Table 1.1.1Q Percent Change From Preceding Period in Real Gross Domestic Product.dta", replace
project, creates("${root}/data/derived/NIPA/NIPA Table 1.1.1Q Percent Change From Preceding Period in Real Gross Domestic Product.dta")

*-------------------------------------------------------------------------------
* NIPA Table 1.1.6 Real Gross Domestic Product, Chained Dollars
*-------------------------------------------------------------------------------

* Load data
load_nipa 1.1.6, freq(Q)
drop tableidlineno seriescodeparents  // not useful to us

* Label value
assert metricname == "Chained Dollars"
assert calculationtype == "Level"
assert defaultscale == -6
label var value "Components of Real GDP, in Millions of Chained 2012 Dollars"
drop metricname calculationtype defaultscale // constant values

* Output
save "${root}/data/derived/NIPA/NIPA Table 1.1.6Q Real Gross Domestic Product in Chained Dollars.dta", replace
project, creates("${root}/data/derived/NIPA/NIPA Table 1.1.6Q Real Gross Domestic Product in Chained Dollars.dta")

*-------------------------------------------------------------------------------
* NIPA Table 2.3.2 Contributions to Percent Change in Real Personal Consumption Expenditures by Major Type of Product
*-------------------------------------------------------------------------------

* Load data
load_nipa 2.3.2, freq(Q)
drop tableidlineno seriescodeparents  // not useful to us

* Label value
assert metricname == "Quantity Contributions" | (serieslabel == "Personal consumption expenditures (PCE)" & metricname == "Fisher Quantity Index")
assert calculationtype == "Level" | (serieslabel == "Personal consumption expenditures (PCE)" & calculationtype == "Percent change, annual rate")
assert defaultscale == 0
label var value "Contribution to real PCE percent change, as annual rate"
drop metricname calculationtype defaultscale // constant values

* Output
save "${root}/data/derived/NIPA/NIPA Table 2.3.2Q Contributions to Percent Change in Real Personal Consumption Expenditures by Major Type of Product.dta", replace
project, creates("${root}/data/derived/NIPA/NIPA Table 2.3.2Q Contributions to Percent Change in Real Personal Consumption Expenditures by Major Type of Product.dta")

*-------------------------------------------------------------------------------
* NIPA Table 2.3.5 Personal Consumption Expenditures by Major Type of Product
*-------------------------------------------------------------------------------

* Load data
load_nipa 2.3.5, freq(M)
drop tableidlineno seriescodeparents  // not useful to us

* Label value
assert metricname == "Current Dollars"
assert calculationtype == "Level"
assert defaultscale == -6
label var value "Nominal Annualized Spending, in Millions"
drop metricname calculationtype defaultscale // constant values

* Output
save "${root}/data/derived/NIPA/NIPA Table 2.3.5M Personal Consumption Expenditures by Major Type of Product.dta", replace
project, creates("${root}/data/derived/NIPA/NIPA Table 2.3.5M Personal Consumption Expenditures by Major Type of Product.dta")

*-------------------------------------------------------------------------------
* NIPA Table 2.3.6 Real Personal Consumption Expenditures by Major Type of Product, Chained Dollars
*-------------------------------------------------------------------------------

* Load data
load_nipa 2.3.6, freq(M) omitseries(D236RX)  // omit "Residual" series, which is not available at Monthly frequency
drop tableidlineno seriescodeparents  // not useful to us

* Label value
assert metricname == "Chained Dollars"
assert calculationtype == "Level"
assert defaultscale == -6
label var value "Real Annualized Spending, in Millions of Chained Dollars"
drop metricname calculationtype defaultscale // constant values

* Output
save "${root}/data/derived/NIPA/NIPA Table 2.3.6M Real Personal Consumption Expenditures by Major Type of Product in Chained Dollars.dta", replace
project, creates("${root}/data/derived/NIPA/NIPA Table 2.3.6M Real Personal Consumption Expenditures by Major Type of Product in Chained Dollars.dta")
